package com.smartcampus.demo.service;

import com.smartcampus.demo.domain.Gas;
import com.smartcampus.demo.repository.GasDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.util.List;

@Service
public class GasService {

    @Autowired
    private GasDao gasDao;

    public void saveGas(Gas gas) {
        // 设置时区为北京时间
        ZonedDateTime beijingTime = ZonedDateTime.now(ZoneId.of("Asia/Shanghai"));
        gas.setTime(beijingTime.toLocalDateTime());
        gasDao.save(gas);
    }

    public List<Gas> getAllGas() {
        return gasDao.findAll();
    }

    public Gas getGasById(Long id) {
        return gasDao.findById(id).orElse(null);
    }

    // 获取最新的 n 个数据
    public List<Gas> getLatestNGas(int n) {
        return gasDao.findTopNByOrderByCreatedAtDesc(n);
    }
}